package com.yaorange.jk.web.cargo;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ModelDriven;
import com.yaorange.jk.entity.Export;
import com.yaorange.jk.entity.ExportProduct;
import com.yaorange.jk.service.ContractService;
import com.yaorange.jk.service.ExportService;
import com.yaorange.jk.utils.FastJsonUtil;
import com.yaorange.jk.utils.Pagination;
import com.yaorange.jk.web.BaseAction;
import com.yaorange.webservice.EpService;
import com.yaorange.webservice.Exception_Exception;
import com.yaorange.webservice.WsExport;
import com.yaorange.webservice.WsExportProduct;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author coach tam
 * @date 2017/11/1
 */
public class ExportAction extends BaseAction implements ModelDriven<Export> {

    /**
     * 因为模型驱动需要实现getModel，然后我们把这个对象传递给jsp 需要单独弄一个get方法。
     * 所以，我们把名字改成model，一个getModel 就搞定了。
     */
    private Export model = new Export();

    //分页对象
    private Pagination page = new Pagination();

    @Autowired
    private ExportService exportService;

    @Autowired
    private ContractService contractService;

    @Autowired
    private EpService epService;

    private String[] mr_id;
    private String[] mr_changed;
    private Integer[] mr_orderNo;
    private Integer[] mr_cnumber;
    private Double[] mr_grossWeight;
    private Double[] mr_netWeight;
    private Double[] mr_sizeLength;
    private Double[] mr_sizeWidth;
    private Double[] mr_sizeHeight;
    private Double[] mr_exPrice;
    private Double[] mr_tax;

    /**
     * 查询待报运合同
     */
    public String contractList(){
        page = contractService.findPage(page.getPageNo(),page.getPageSize(), 1);
        page.setUrl("exportAction_contractList");
        push(page);
        return "contractList";
    }

    /**
     * 分页查询
     */
    public String list() {
        page = exportService.findPage(page.getPageNo(), page.getPageSize());
        page.setUrl("exportAction_list");
        push(page);
        return "list";
    }

    /**
     * 报运
     */
    public String tocreate() {

        return "tocreate";
    }

    /**
     * 添加
     */
    public String insert() {
        exportService.save(model);
        return contractList();
    }

    /**
     * 跳转更新页面
     * @return
     */
    public String toupdate() {
        //根据id查询部门老数据
        model = exportService.findById(model.getId());
        push(model);
        return "toupdate";
    }

    /**
     *更新
     */
    public String update() {
        exportService.update(model,mr_id,mr_changed,mr_orderNo,mr_cnumber,mr_grossWeight,mr_netWeight,mr_sizeLength,mr_sizeWidth,mr_sizeHeight,mr_exPrice,mr_tax);
        return "rlist";
    }

    /**
     * 批量删除
     */
    public String delete() {
        String[] ids = model.getId().split(", ");
        exportService.deleteByIds(ids);
        return "rlist";
    }


    public String getExportProductJson(){
        //根据报运单id 查询出报运货物
        Export export = exportService.findById(model.getId());
        Set<ExportProduct> epSet = export.getExportProducts();

        //set --> json
//        String json = FastJsonUtil.toJSONString(epSet);
        String json = JSON.toJSONString(epSet);

        FastJsonUtil.write_json(ServletActionContext.getResponse(),json);
        return NONE;
    }

    public String toview(){
        model = exportService.findById(model.getId());
        push(model);
        return "toview";
    }
    
    public String cancel(){
    	String[] ids = model.getId().split(", ");
    	for (String string : ids) {
    		Export export=exportService.findById(string);
    		export.setState(0);//重置export状态为0，草稿
    		exportService.update(export);
		}
    	return list();
    }
    /**
     * 电子报运
     * @return
     */
    public String export(){
        //准备报运所需数据json
        //1,Export-->WsExport

        Export export = exportService.findById(model.getId());
        WsExport wsExport = new WsExport();
        try {
            //报运信息拷贝
            BeanUtils.copyProperties(wsExport,export);
            wsExport.setExportId(export.getId());

            //报运货物信息拷贝
            Set<WsExportProduct> products = new HashSet<>();
            Set<ExportProduct> exportProducts = export.getExportProducts();
            //exportProducts 拷贝到 products中
            for (ExportProduct ep :exportProducts)
            {
                WsExportProduct wsEp = new WsExportProduct();
                //ep-->wsEp
                BeanUtils.copyProperties(wsEp,ep);

                wsEp.setExportProductId(ep.getId());
                products.add(wsEp);
            }

            wsExport.setProducts(products);


            //2,WsExport --> json
            String json = FastJsonUtil.toJSONString(wsExport);
            String result = epService.exportE(json);

            System.out.println(result);

            //3,修改报运单状态
            export.setState(2);
            exportService.update(export);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (Exception_Exception e) {
            e.printStackTrace();
        }

        return "rlist";
    }

    public void setPage(Pagination page) {
        this.page = page;
    }

    public void setMr_id(String[] mr_id) {
        this.mr_id = mr_id;
    }

    public void setMr_changed(String[] mr_changed) {
        this.mr_changed = mr_changed;
    }

    public void setMr_orderNo(Integer[] mr_orderNo) {
        this.mr_orderNo = mr_orderNo;
    }

    public void setMr_cnumber(Integer[] mr_cnumber) {
        this.mr_cnumber = mr_cnumber;
    }

    public void setMr_grossWeight(Double[] mr_grossWeight) {
        this.mr_grossWeight = mr_grossWeight;
    }

    public void setMr_netWeight(Double[] mr_netWeight) {
        this.mr_netWeight = mr_netWeight;
    }

    public void setMr_sizeLength(Double[] mr_sizeLength) {
        this.mr_sizeLength = mr_sizeLength;
    }

    public void setMr_sizeWidth(Double[] mr_sizeWidth) {
        this.mr_sizeWidth = mr_sizeWidth;
    }

    public void setMr_sizeHeight(Double[] mr_sizeHeight) {
        this.mr_sizeHeight = mr_sizeHeight;
    }

    public void setMr_exPrice(Double[] mr_exPrice) {
        this.mr_exPrice = mr_exPrice;
    }

    public void setMr_tax(Double[] mr_tax) {
        this.mr_tax = mr_tax;
    }

    @Override
    public Export getModel() {
        return model;
    }
}
